package com.work.game.ad.kaku.nativie;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeEventListener;
import com.anythink.nativead.api.ATNativeNetworkListener;
import com.anythink.nativead.api.NativeAd;

import cn.hutool.core.util.ObjectUtil;

/**
 * 信息流
 */
public class TaKuNativeAd implements ATNativeEventListener {
    private static String TAG = "TaKuNativeAd";
    private static volatile TaKuNativeAd instance;

    private ATNative atNative;

    private ATNativeAdView mATNativeAdView; //渲染广告必须创建的容器
    private NativeAd mNativeAd;
    private Context context;

    private TaKuNativeAd() {
    }

    public static synchronized TaKuNativeAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (TaKuNativeAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new TaKuNativeAd();
                }
            }
        }
        return instance;
    }

    public void loadAd(Context context, String placementId) {
        this.context = context;
        //初始化广告加载对象
        atNative = new ATNative(context, placementId, new ATNativeNetworkListener() {
            @Override
            public void onNativeAdLoaded() {
                Log.i(TAG, "onNativeAdLoaded");
            }

            @Override
            public void onNativeAdLoadFail(AdError adError) {
                //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
                //AdError，请参考 https://docs.takuad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.i(TAG, "onNativeAdLoadFail:" + adError.getFullErrorInfo());
            }
        });
        //发起广告请求
        atNative.makeAdRequest();

    }


    public void showAd(Activity activity) {
        if (atNative == null) {
            return;
        }

        if (!atNative.checkAdStatus().isReady()) {
            return;
        }

        NativeAd nativeAd = atNative.getNativeAd();
        //开发者可以在调用getNativeAd后直接使用ATNative#makeAdRequest发起预加载下一次的广告
        //loadNativeAd();

        if (nativeAd != null) {
            if (mNativeAd != null) {
                mNativeAd.destory();
            }
            mNativeAd = nativeAd;
            mNativeAd.setNativeEventListener(this);
            mNativeAd.renderAdContainer(mATNativeAdView, null);
            mNativeAd.prepare(mATNativeAdView, null);
//            if (!nativeAd.isNativeExpress()) {
//                //自渲染
//                //nativePrepareInfo = new ATNativePrepareInfo();
//
//            } else {
//                //模板渲染
//                nativeAd.renderAdContainer(mATNativeAdView, null);
//            }
            //nativeAd.prepare(mATNativeAdView, nativePrepareInfo);
        }
    }

    public void setmATNativeAdView(ATNativeAdView mATNativeAdView) {
        this.mATNativeAdView = mATNativeAdView;
    }

    @Override
    public void onAdImpressed(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {

    }

    @Override
    public void onAdClicked(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {

    }

    @Override
    public void onAdVideoStart(ATNativeAdView atNativeAdView) {

    }

    @Override
    public void onAdVideoEnd(ATNativeAdView atNativeAdView) {

    }

    @Override
    public void onAdVideoProgress(ATNativeAdView atNativeAdView, int i) {

    }
}
